﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Windows.Forms;
using V5_Apps.Core;
using V5_Apps.Plugins.Qvod._Class;
using V5_Apps.Plugins.Qvod._Class.Model;
using WeifenLuo.WinFormsUI.Docking;
using XmlDatabase.Core;

namespace V5_Apps.Plugins.Qvod {
    public partial class frmMain : DockContent, IApp {
        public frmMain() {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e) {

        }

        private void button3_Click(object sender, EventArgs e) {
            //var qvod = new ToQvod.qvodSoapClient();
            //var msg = qvod.QvodTo(this.txtNetUrl2.Text);
            //MessageBox.Show(msg);
        }

        private void btnGetQvod_Click(object sender, EventArgs e) {
            string encode = string.Empty;
            string url = this.txtQvodUrl.Text;
            this.txtQvodResult.Text = string.Empty;
            if (string.IsNullOrEmpty(url)) {
                this.txtQvodResult.AppendText("Url地址不能为空!");
                return;
            }
            ThreadMultiHelper th = new ThreadMultiHelper(1, 1);
            th.WorkMethod = delegate(int a, int b) {
                using (XDatabase db = XDatabase.Open(AppHelper.Instance.GetAppPath("Qvod") + "Spider")) {
                    var query = db.Query<ModelSpiderModule>();
                    if (query != null && query.Count() > 0) {
                        foreach (var model in query.ToList()) {
                            string result = QvodHelper.Instance.ToUrl(model, string.Empty, ref encode, url, true);
                            if (!string.IsNullOrEmpty(result)) {
                                this.txtQvodResult.AppendText(result);
                                break;
                            }
                        }
                    }
                }
            };
            th.CompleteEvent = delegate() {
                this.txtQvodResult.AppendText("\r\n");
                this.txtQvodResult.AppendText("解析完成!");
            };
            th.Start();
        }

        private void 模块管理ToolStripMenuItem_Click(object sender, EventArgs e) {
            ModManage();
        }

        private void toolStripButton_ModManage_Click(object sender, EventArgs e) {
            ModManage();
        }

        private void ModManage() {
            new frmTest().ShowDialog(this);
        }

        private void button1_Click_1(object sender, EventArgs e) {

        }

        public ToolStripMenuItem[] GetMenuItem() {
            return null;
        }

        public ToolStripItem[] GetToolBarItem() {
            throw new NotImplementedException();
        }

        public ToolStripItem[] GetStatusItem() {
            throw new NotImplementedException();
        }
    }
}
